Skip to content

add zig escrow example#542

Open
Perelyn-sama wants to merge 1 commit into
solana-developers:mainfrom
Perelyn-sama:zig/escrow
Open

add zig escrow example#542
Perelyn-sama wants to merge 1 commit into
solana-developers:mainfrom
Perelyn-sama:zig/escrow

Conversation

@Perelyn-sama
Copy link
Copy Markdown
Collaborator

No description provided.

@Perelyn-sama Perelyn-sama requested a review from dev-jodee May 30, 2026 07:22
Copy link
Copy Markdown
Collaborator

@dev-jodee dev-jodee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work,

Seems like the litesvm.rs test file is completely empty, we should have tests

We should add a README.md like the other examples

program.root_module.addImport("solana_program_library", solana_lib_mod);

_ = solana.buildProgram(b, program, target, optimize);
b.installArtifact(program);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this redundant because of the following steps ?

"build.zig",
"build.zig.zon",
"src",
// For example...
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this is boilerplate we can remove

// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
// Together with name, this represents a globally unique package
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need all the boilerplate from the zig init call? Maybe we should clean this up since it's a program example for solana?

output_dir="solana-zig"
fi
output_dir="$(mkdir -p "$output_dir"; cd "$output_dir"; pwd)"
cd $output_dir
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think if it's unquoted it might crash on paths with spaces

}

pub fn take_offer(program_id: *PublicKey, accounts: []Account) ProgramError!void {
if (!(accounts.len == 10)) return ProgramError.InvalidAcctData;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like you're accessing accounts up to 12 below

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nit: if !(==) can just be !=

}) catch return error.Unexpected;

sol_lib.token.transfer(.{ .from = vault.info(), .to = taker_token_account_a.info(), .amount = vault_amount_a, .authority = .{ .multiple = &[_]Account.Info{
offer_info.info(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this should just be .single = offer_info.info() no? The ATA auth is the Offer PDA?

pub const InstructionType = enum(u8) { make, take };

pub fn processInstruction(program_id: *PublicKey, accounts: []Account, data: []const u8) ProgramError!void {
const instruction_type: *const InstructionType = @ptrCast(data);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check length here?


switch (instruction_type.*) {
InstructionType.make => {
const make_data: *align(1) const MakeOffer = @ptrCast(data[1..]);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check length of data here as well

const rent_info = accounts[9];

if (!maker.isSigner()) return ProgramError.InvalidAcctData;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think payer needs to be checked for signer too (in the other insturction as well)

try assert_ata(taker_token_account_a.id(), taker.id(), token_mint_a.id());

if (taker_token_account_a.lamports().* == 0) {
// TODO; improve error handling
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be done in the example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants